/**
 * @ author: 汪孟苹
 */
define(["jquery","oms_config/root","bbcare","fu/app/comm/page/page",
	"scprit/followup/app/workBench/workBench.servers.js",
	"scprit/followup/app/workBench/workBench.ui.js"
	],
	function($,$root,$bb,$page,$workBenchServers,$workBenchUi){
	var event = {};
	/**
	 * 点击"添加患者"按钮跳转到"添加患者"页面
	 */
	event.onAddPatientClick = function(){
		$bb.selectNavbar("addPatient");
	}; 
	/**
	 * 点击今日门诊患者跳转
	 */
	event.onBabyClick = function(){
		var rows = $(this).data("value");
		var index = $(this).index();
		var imgName = rows[index].gender == 'N'? 'boy' : 'girl';
		var imgSrc = "page/followup/imgs/comm/"+ imgName +".png";
		var params = {
			patientSchemeId: rows[index].ptSchemeid,
			followQueueId: rows[index].followQueueId,
			patientName: rows[index].patientName,
			imgSrc: imgSrc,
			queuename: rows[index].followQueueName,
			patientId: rows[index].patientId,
			taskId: rows[index].taskId,
		};
		$bb.openPage("outPatient",params);
	}
	/**
	 * 窗口期计划随访患者/计划随访患者（tit根据flag不同有所不同）
	 */
	var windowTitText = function(flag){
		var config = $root.init();
		var text = (flag == "2"? config.language.workBenchfollowpatient : config.language.workBenchWindowplansfollowpatient);
		$("#windowTit").html(text);	
	};
	/**
	 * 按科研编号、手机号、姓名搜索患者时，传参有所不同
	 */
	var searchKeywordAjax = function(params){
		var reg = /^1\d{10}$/;
		if(reg.test(params.fuzzyVar)){
			params = $.extend(params,{phoneNum:params.fuzzyVar});
		}
		$workBenchServers.ajaxPatientsQueue(params,ajaxPatientsQueueCallBack);
	};
	/**
	 * 点击姓按钮进行搜索
	 */
	onFirstNameClick = function(){
		var params = $(this).data();
		var keyword = $(this).html();
		var params = {
			"flag":"0",
			"page":1,
			"rows":params.rows,
			"patientName":keyword
		}
		windowTitText(params.flag);
		$workBenchServers.ajaxPatientsQueue(params,ajaxPatientsQueueCallBack);
	}
	/**
	 * 输入姓名、手机号、科研编号进行搜索
	 */
	onKeywordClick = function(){
		var params = $(this).data();
		$(this).prev().val("");
		var keyword = $(this).prev().val();
		var params = {
			"flag":"0",
			"page":1,
			"rows":params.rows,
			"fuzzyVar":keyword
		}
		windowTitText(params.flag);
		searchKeywordAjax(params);	
	}
	onKeywordInput = function(){
		var params = $(this).data();
		var keyword = $(this).val();
		var flag = (keyword ==""? "0" : "2");
		var params = {
			"flag":flag,
			"page":1,
			"rows":params.rows,
			"fuzzyVar":keyword
		}
		windowTitText(params.flag);
		searchKeywordAjax(params);
	}
	/**
	 * 改变队列进行搜索
	 */
	onQueueChange = function(){
		var params = $(this).data();
		var queueId = $(this).val();
		var params = {
			"flag":"0",
			"page":1,
			"rows":params.rows,
			"queueId":queueId
		}
		windowTitText(params.flag);
		$workBenchServers.ajaxPatientsQueue(params,ajaxPatientsQueueCallBack);
	}
	/**
	 * 点击随访患者卡片进入详情页
	 */
	var onBabyCardClick = function(){
		var followQueueId = $(this).attr("followQueueId");
		var patientSchemeId = $(this).attr("patientSchemeId");
		var patientId = $(this).attr("patientId");
		var imgSrc = $(this).find(".clickbabycard-icon").find("img").attr("src");
		var patientName = $(this).find(".clickbabycard-name").text();
		var queuename = $(this).find(".clickbabycard-queuename").text();
		var params = {
			patientSchemeId: patientSchemeId,
			followQueueId: followQueueId,
			patientName: patientName,
			imgSrc: imgSrc,
			queuename: queuename,
			patientId: patientId
		};
		$bb.openPage("outPatient",params);
	};

	/**
	 * 请求窗口期计划随访患者成功后的回调函数
	 */
	var ajaxPatientsQueueCallBack = function(data,params){
		if(data.rows && data.rows.length > 0){
			var pagesAll = Math.ceil(data.total / params.rows);
			if(data.FirstName){
				$workBenchUi.firstNameInit({
					id:"windowFirstName",
					list:data.FirstName,
					rows:params.rows
				});
				/* 姓查询 */
				$("#windowFirstName button").unbind("click");
				$("#windowFirstName button").data(params);
				$("#windowFirstName button").click(onFirstNameClick);
			}
			$workBenchUi.babyListInit({
				id:"windowBabyList",
				list:data.rows,
				rows:params.rows
			});
			/* 翻页 */
			$page.turnInit({
				id:"windowBottomPage",
				pagesAll:pagesAll,
				page:params.page
			},$workBenchServers.ajaxPatientsQueue,params,ajaxPatientsQueueCallBack);
			/* 点击随访患者卡片进入详情页 */
			$("#windowBabyList .clickbabycard").unbind("click");
			$("#windowBabyList .clickbabycard").click(onBabyCardClick);
		}else{
			$("#windowBabyList").html('<div class="col-sm-12 col-md-12 babycard-null">暂无数据..</div>');
			$("#windowBottomPage").html("");
		}
		/* 姓名、科研编号、手机号码查询 */
		$("#windowKeyword span").unbind("click");
		$("#windowKeyword span").data(params);
		$("#windowKeyword span").click(onKeywordClick);
		$("#windowKeyword input").unbind("input propertychange");
		$("#windowKeyword input").data(params);
		$("#windowKeyword input").bind("input propertychange",onKeywordInput);
		/* 根据队列名称查询 */
		$("#selectQueue select").unbind("change");
		$("#selectQueue select").data(params);
		$("#selectQueue select").change(onQueueChange);	
	};
	event.ajaxPatientsQueueCallBack = function(data,params){
		ajaxPatientsQueueCallBack(data,params);
	}
	
	return event;
});
